<template>
	<view class="content">
		<uni-search-bar placeholder="请输入物品名称" @confirm="search(searchName)" @cancel="cancel" cancel-text="取消" v-model="searchName">
			<template v-slot:searchIcon> 
				<uni-icons  color="#999999" size="18" type="search" />
			</template>
		</uni-search-bar>
		<button @click="getAll()">刷新</button>
		<uni-list v-for="(item,index) in listData" :key="index" >			
			<uni-list-item :title="item.name"  :note="item.remark" :rightText="item.type" />
			<view style="font-size: 90%;">
				操作时间：
				<input style="font-size: 90%;" disabled v-model="item.time"/>
			</view>
		</uni-list>
		<!-- <button @click="add">添加记录</button> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				listData:[],
				searchName: ''

			}
		},
		onLoad() {
			this.getAll();
		},
		methods: {
			// 查询SQL语句
			getAll(){
				let that = this; // 备份 Vue 实例
				plus.sqlite.selectSql({
					name: 'CangKu',
					sql: 'select * from record',
					success: function(data){
						console.log('selectSql success: ');
						for(var i in data){
							console.log(data[i]);
						}
						that.listData = data
						return uni.showToast({
							title:'查询成功！!',
							icon:'success'
						})
						
					},
					fail: function(e){
						console.log('selectSql failed: '+JSON.stringify(e));
					}
				});
			},			
					
			add(){
				const sqlRecord = `INSERT INTO record (name, num, remark,type) VALUES ('测试', 2, '无','出库')`
				plus.sqlite.executeSql({
					name: 'CangKu',
					sql: sqlRecord,
					success: function(e){
						console.log('insertRecord success!');
					},
					fail: function(e){
						console.log('insertRecord failed: '+JSON.stringify(e));
					}
				});
				this.getAll()
			},
			search(searchName){
				let that = this; // 备份 Vue 实例
				const sql = `select  * from record where name LIKE '%${searchName}%'`;
				plus.sqlite.selectSql({
					name: 'CangKu',
					sql: sql,
					success: function(data){
						console.log('selectSql success: ');
						for(var i in data){
							console.log(data[i]);
						}
						that.listData = data
						return uni.showToast({
							title:'查询成功！!',
							icon:'success'
						})
						
					},
					fail: function(e){
						console.log('selectSql failed: '+JSON.stringify(e));
					}
				});
			}

		}
	}
</script>

<style>

</style>
